package com.group2.view;

import com.group2.util.StringUtil;
import model.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.*;
import java.net.Socket;
import static model.netBase.IP;


/*用户管理模块：
 * 学生密码修改*/

public class UserPasswordModifyFrame extends JFrame {

	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JButton jb_modify;
	private JButton jb_reset;
	private JPasswordField newPasswordTxt;
	private JPasswordField oldPasswordTxt;
	private JPasswordField passwordConfirmTxt;
	private Timer timer;

	private String currentName;

	public UserPasswordModifyFrame(String name, Timer time) {
		currentName=name;
		timer=time;
		if(time!=null) {
			time.start();
			this.addMouseMotionListener(new MouseMotionListener() {
				@Override
				public void mouseDragged(MouseEvent e) {
				}

				@Override
				public void mouseMoved(MouseEvent e) {
					time.stop();
				}
			});
			this.addMouseListener(new MouseListener() {
				@Override
				public void mouseClicked(MouseEvent e) {
				}

				@Override
				public void mousePressed(MouseEvent e) {
				}

				@Override
				public void mouseReleased(MouseEvent e) {
				}

				@Override
				public void mouseEntered(MouseEvent e) {
					time.start();
				}

				@Override
				public void mouseExited(MouseEvent e) {
					time.start();
				}
			});
		}
		Font font = new Font("Dialog", Font.PLAIN, 14);
		this.setLocationRelativeTo(null);
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, font);
			}
		}
		initComponents();
	}

	private void initComponents() {

		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jb_modify = new JButton();
		jb_reset = new JButton();
		oldPasswordTxt = new JPasswordField();
		newPasswordTxt = new JPasswordField();
		passwordConfirmTxt = new JPasswordField();

		//setClosable(true);
		//setIconifiable(true);
		setTitle("用户密码修改");

		jLabel1.setText("原密码:");
		jLabel2.setText("新密码:");
		jLabel3.setText("再次输入新密码:");

		jb_modify.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/modify.png")));
		jb_modify.setText("修改");
		jb_modify.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jb_modifyActionPerformed(evt);
			}
		});

		jb_reset.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/reset.png"))); 
		jb_reset.setText("重置");
		jb_reset.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jb_resetActionPerformed(evt);
			}
		});
		
		int wW=this.getWidth();
		int wH=this.getHeight();
		Toolkit kit=Toolkit.getDefaultToolkit();
		Dimension screenSize=kit.getScreenSize();
		int sW=screenSize.width;
		int sH=screenSize.height;
		//this.setLocation(sW/2-wW/2,sH/2-wH/2);
		this.setLocationRelativeTo(null);
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
				.createSequentialGroup().addGap(88, 88, 88)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel3)
						.addComponent(jLabel2).addComponent(jLabel1).addComponent(jb_modify))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
						.addComponent(newPasswordTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 152,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(passwordConfirmTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 152,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(oldPasswordTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 152,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(jb_reset))
				.addContainerGap(105, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
				.createSequentialGroup().addGap(28, 28, 28)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
						.addComponent(oldPasswordTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(28, 28, 28)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2)
						.addComponent(newPasswordTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(18, 18, 18)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel3)
						.addComponent(passwordConfirmTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jb_modify)
						.addComponent(jb_reset))
				.addGap(25, 25, 25)));

		pack();
		

	}

	private void jb_modifyActionPerformed(ActionEvent evt) {
		String userName=this.currentName;
		String oldPassword = new String(this.oldPasswordTxt.getPassword());
		String newPassword = new String(this.newPasswordTxt.getPassword());
		String passwordConfirm = new String(this.passwordConfirmTxt.getPassword());
		if (StringUtil.isEmpty(oldPassword)) {
			JOptionPane.showMessageDialog(this, "原密码不能为空");
			return;
		}
		if (StringUtil.isEmpty(newPassword)) {
			JOptionPane.showMessageDialog(this, "新密码不能为空!");
			return;
		}
		if (!StringUtil.isPassword(newPassword)) {
			JOptionPane.showMessageDialog(this, "密码长度需在8~16位之间，包含字母、数字、特殊字符!");
			return;
		}
		if (StringUtil.isEmpty(passwordConfirm)) {
			JOptionPane.showMessageDialog(this, "请再次输入新密码!");
			return;
		}
//		String rightOldPassword = UserLogOnFrame.currentStudent.getSpassword();
//		if (!oldPassword.equals(rightOldPassword)) {
//			JOptionPane.showMessageDialog(this, "新老密码不能重复!");
//			return;
//		}
		if (newPassword.equals(passwordConfirm)) {
			try {
				Socket socket = new Socket(IP(), 2000);
				//设置超时时间
				socket.setSoTimeout(3000);
				//构建IO
				InputStream is = socket.getInputStream();
				OutputStream os = socket.getOutputStream();

				//向服务器传输操作码、用户名、旧密码、新密码
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
				bw.write("13"+'\n'+currentName+'\n'+oldPassword+'\n'+passwordConfirm+'\n');
				bw.flush();
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				String str = br.readLine();
				boolean flag=Boolean.parseBoolean(str);
				System.out.println(str);
				if(flag){
					JOptionPane.showMessageDialog(this, "成功修改密码!");
					this.dispose();
					new UserLogOnFrame().setVisible(true);
				}
				else{
					JOptionPane.showMessageDialog(this, "原密码错误！");
				}
				is.close();
				os.close();
				socket.close();
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "连接异常!");
			}

		}
//		Connection con = null;
//		int currentSno = UserLogOnFrame.currentStudent.getSno();
//		Student student = new Student(currentSno, newPassword);

		/*try {
			con = dbUtil.getCon();
			int modifyNum = studentDao.PasswordModify(con, student);
			if (modifyNum == 1) {
				JOptionPane.showMessageDialog(this, "……");
				this.resetValue();

			} else {
				JOptionPane.showMessageDialog(this, "……");
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "……");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}*/
	}

	private void resetValue() {
		this.oldPasswordTxt.setText("");
		this.newPasswordTxt.setText("");
		this.passwordConfirmTxt.setText("");
	}

	private void jb_resetActionPerformed(ActionEvent evt) {
		this.oldPasswordTxt.setText("");
		this.newPasswordTxt.setText("");
		this.passwordConfirmTxt.setText("");
	}

}